summaryrefslogtreecommitdiff
path: root/src/pages/shop/product/[slug].js
blob: 10b4c322e73a3569ed75b8e0c6ed657dadff801e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import Link from "next/link";
import { useRouter } from "next/router";
import { useEffect, useState } from "react";
import Header from "../../../components/Header";
import ProductCard from "../../../components/ProductCard";
import { getOdoo } from "../../../helpers/apiOdoo";
import { createSlug, getId } from "../../../helpers/slug";
import currencyFormat from "../../../helpers/currencyFormat";
import Head from "next/head";
import { Swiper, SwiperSlide } from "swiper/react";
import { LazyLoadImage } from "react-lazy-load-image-component";

import 'swiper/css';
import 'react-lazy-load-image-component/src/effects/blur.css';

import ImagePlaceholderIcon from '../../../icons/image-placeholder.svg';


export async function getServerSideProps(context) {
  const { slug } = context.query;
  let product = await getOdoo('/api/v1/product/' + getId(slug));
  product = product[0];
  return {props: {product}};
}

export default function ProductDetail({product}) {
  const router = useRouter();
  const { slug } = router.query;
  const [selectedVariant, setSelectedVariant] = useState("");
  const [quantity, setQuantity] = useState("1");
  const [similarProducts, setSimilarProducts] = useState(null);
  const [activeVariant, setActiveVariant] = useState({
    id: product.id,
    code: product.code,
    price: product.lowest_price,
    stock: product.stock_total,
    weight: product.weight,
    attributes: '',
  });
  
  useEffect(() => {
    if (product.variants.length == 1) {
      setSelectedVariant(product.variants[0].id);
    }
  }, [product]);

  useEffect(() => {
    setSimilarProducts(null);
    const getSimilarProducts = async () => {
      const dataSimilarProducts = await getOdoo(`/api/v1/product/${getId(slug)}/similar?limit=20`);
      setSimilarProducts(dataSimilarProducts);
    }
    getSimilarProducts();
  }, [slug]);

  useEffect(() => {
    if (selectedVariant != '') {
      let newActiveVariant = product.variants.filter((variant) => {
        return variant.id == selectedVariant;
      });
  
      if (newActiveVariant.length == 1) {
        newActiveVariant = newActiveVariant[0];
        setActiveVariant({
          id: newActiveVariant.id,
          code: newActiveVariant.code,
          price: newActiveVariant.price,
          stock: newActiveVariant.stock,
          weight: newActiveVariant.weight,
          attributes: newActiveVariant.attributes.join(', '),
        });
      }
    }
  }, [selectedVariant, product]);

  let onchangeVariant = (e) => {
    setSelectedVariant(e.target.value);
    setQuantity("1");
  }

  let addToCart = () => {
    return true;
  }

  return (
    <>
      <Head>
        <title>{product.name + '- Indoteknik'}</title>
      </Head>
      <Header />
      <div className="px-4 pt-5 pb-10">
        <LazyLoadImage effect="blur" src={product.image} alt={product.name} className="border border-gray-300 rounded-md mb-4 w-full h-[300px] object-contain object-center" />
        <Link href={'/shop/brands/' + createSlug(product.manufacture.name, product.manufacture.id)}>
          {product.manufacture.name || '-'}
        </Link>
        <h1 className="mb-3">{product.name}{activeVariant.attributes != '' ? ' - ' + activeVariant.attributes : ''}</h1>

        {product.variant_total > 1 && selectedVariant == "" ? (
          <p className="text-xs text-gray-800 mb-1">Harga mulai dari:</p>
        ) : ''}

        {product.lowest_price.discount_percentage > 0 ? (
          <div className="flex gap-x-1 items-center">
            <span className="badge-yellow">{activeVariant.price.discount_percentage}%</span>
            <p className="text-xs text-gray-800 line-through">{currencyFormat(activeVariant.price.price)}</p>
          </div>
        ) : ''} 

        {product.lowest_price.price > 0 ? (
          <p className="text-lg text-gray-900 font-semibold">{currencyFormat(activeVariant.price.price_discount)}</p>
        ) : (
          <p className="text-gray-800">Dapatkan harga terbaik, <a href="">hubungi kami.</a></p>
        )}

        <div className="flex gap-x-2 mt-5">
          <div className="w-9/12">
            <label className="form-label">Pilih: <span className="text-gray-800">{product.variant_total} Varian</span></label>
            <select name="variant" className="form-input" value={selectedVariant} onChange={onchangeVariant} >
              <option value="" disabled={selectedVariant != "" ? true : false}>Pilih Varian...</option>
              {product.variants.length > 1 ? (
                product.variants.map((variant) => {
                  return (
                    <option key={variant.id} value={variant.id}>{variant.attributes.join(', ')}</option>
                  );
                })
              ) : (
                <option key={product.variants[0].id} value={product.variants[0].id}>{product.variants[0].name}</option>
              )}
            </select>
          </div>
          <div className="w-3/12">
            <label htmlFor="quantity" className="form-label">Jumlah</label>
            <input type="number" name="quantity" id="quantity" className="form-input text-center is-invalid" value={quantity} onChange={(e) => setQuantity(e.target.value)} />
          </div>
        </div>

        <div className="flex gap-x-2 mt-2">
          <button className="btn-light w-full" >+ Quotation</button>
          <button className="btn-primary w-full" onClick={addToCart} disabled={(product.lowest_price.price == 0 ? true : false)}>+ Keranjang</button>
        </div>

        <div className="mt-10">
          <h2 className="h1 mb-2">Detail Produk</h2>
          <div className="flex py-2 justify-between items-center gap-x-1 border-b border-gray-300">
            <h3 className="text-gray-900">Jumlah Varian</h3>
            <p className="text-gray-800">{product.variant_total} Varian</p>
          </div>
          <div className="flex py-2 justify-between items-center gap-x-1 border-b border-gray-300">
            <h3 className="text-gray-900">Nomor SKU</h3>
            <p className="text-gray-800" id="sku_number">SKU-{activeVariant.id}</p>
          </div>
          <div className="flex py-2 justify-between items-center gap-x-1 border-b border-gray-300">
            <h3 className="text-gray-900">Part Number</h3>
            <p className="text-gray-800" id="part_number">{activeVariant.code}</p>
          </div>
          <div className="flex py-2 justify-between items-center gap-x-1 border-b border-gray-300">
            <h3 className="text-gray-900">Stok</h3>
            <p className="text-gray-800" id="stock">
              {activeVariant.stock > 0 ? (activeVariant.stock > 5 ? 'Lebih dari 5' : 'Kurang dari 5') : '0'}
            </p>
          </div>
          <div className="flex py-2 justify-between items-center gap-x-1 border-b border-gray-300">
            <h3 className="text-gray-900">Berat Barang</h3>
            <p className="text-gray-800" id="weight">{activeVariant.weight > 0 ? activeVariant.weight : '1'} KG</p>
          </div>
        </div>

        <div className="mt-10">
          <h2 className="h1 mb-4">Deskripsi Produk</h2>
          <div className="text-gray-800 leading-7" dangerouslySetInnerHTML={{__html: (product.description.trim() != '' ? product.description.replaceAll(/<*b>/g, '') : 'Belum ada deskripsi produk.')}}></div>
        </div>

        <div className="mt-10">
          <h2 className="h1 mb-4">Produk Lainnya</h2>
          <Swiper freeMode={true} slidesPerView={2.15} spaceBetween={8} loop={true}>
            {similarProducts?.products?.map((product, index) => (<SwiperSlide key={index}><ProductCard data={product} /></SwiperSlide>))}
          </Swiper>
          {similarProducts == null ? (
            <div className="grid grid-cols-2 gap-x-4">
              <div role="status" className="p-4 max-w-sm rounded border border-gray-300 shadow animate-pulse md:p-6">
                <div className="flex justify-center items-center mb-4 h-48 bg-gray-300 rounded">
                <ImagePlaceholderIcon className="w-12 h-12 text-gray-200" />
                </div>
                <div className="h-2 bg-gray-200 rounded-full w-10 mb-1"></div>
                <div className="h-2.5 bg-gray-200 rounded-full w-full mb-4"></div>
                <div className="h-2 bg-gray-200 rounded-full mb-2.5"></div>
                <div className="h-2 bg-gray-200 rounded-full mb-2.5"></div>
                <div className="h-2 bg-gray-200 rounded-full"></div>
                <span className="sr-only">Loading...</span>
              </div>
              <div role="status" className="p-4 max-w-sm rounded border border-gray-300 shadow animate-pulse md:p-6">
                <div className="flex justify-center items-center mb-4 h-48 bg-gray-300 rounded">
                  <ImagePlaceholderIcon className="w-12 h-12 text-gray-200" />
                </div>
                <div className="h-2 bg-gray-200 rounded-full w-10 mb-1"></div>
                <div className="h-2.5 bg-gray-200 rounded-full w-full mb-4"></div>
                <div className="h-2 bg-gray-200 rounded-full mb-2.5"></div>
                <div className="h-2 bg-gray-200 rounded-full mb-2.5"></div>
                <div className="h-2 bg-gray-200 rounded-full"></div>
                <span className="sr-only">Loading...</span>
              </div>
            </div>
          ) : ''}
        </div>

      </div>
    </>
  );
}